package com.rocketmq.mq.comsumer;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.consumer.ConsumeFromWhere;
import org.apache.rocketmq.common.message.Message;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

/****************************************
 * 作者：HuMaSiYuan
 * 邮箱：siyueguoji@163.com
 * 日期：2023/10/10 15:39
 * 功能描述：
 ****************************************/
@Component
public class Consumer implements CommandLineRunner {


    @Value("${rocketmq.consumer.pushConsumer}")
    private String pushConsumer;

    @Value("${rocketmq.namesrvAddr}")
    private String namesrvAddress;


    /**
     * 初始化RocketMq的监听信息，渠道信息
     */
    public void messageListener() {
        DefaultMQPushConsumer consumer =new DefaultMQPushConsumer("SpringBootRocketMqGroup");
        consumer.setNamesrvAddr(namesrvAddress);

        try {
            // 订阅PushTopic下Tag为push的消息,都订阅消息
            consumer.subscribe("PushTopic", "push");
            // 程序第一次启动从消息队列头获取数据
            consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
            // 可以修改每次消费消息的数量, 默认设置是每次消费一条
            consumer.setConsumeMessageBatchMaxSize(1);
            // 在此监听中消费信息，并返回消费的状态信息
            consumer.registerMessageListener((MessageListenerConcurrently)(msgCollection, context) -> {
                // 会把不同的消息分别放置到不同的队列中
                for (Message message : msgCollection) {
                    System.out.println("接收到了消息："+new String(message.getBody()));
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            });
            consumer.start();
        }
        catch (Exception e) {
            e.printStackTrace();
        }


    }

    @Override
    public void run(String... args) throws Exception {
        this.messageListener();
    }
}
